import uuid

from django.db import models
from dvadmin.utils.models import CoreModel, table_prefix
from dvadmin.system.models import SysDict, DeviceTypes, Department, Structures


class DeviceModel(CoreModel):
    modelname = models.CharField(max_length=20, verbose_name='设备型号', help_text="设备型号", null=True, blank=True)
    brand = models.CharField(max_length=20, verbose_name='设备品牌', help_text="设备品牌", null=True, blank=True)
    driverpath = models.CharField(max_length=60, verbose_name="驱动地址", help_text="驱动地址", null=True, blank=True)
    departmentid = models.ForeignKey(Department, db_column='departmentid', on_delete=models.CASCADE,
                                     verbose_name="生产厂商", help_text="生产厂商", null=True, blank=True)
    devicetypeid = models.IntegerField(verbose_name="设备类型ID", help_text="设备类型ID", null=True, blank=True)

    class Meta:
        db_table = table_prefix + 'devicemodel'
        verbose_name = "设备型号信息表"
        verbose_name_plural = verbose_name
        ordering = ('modelname',)


class Devices(CoreModel):
    startplno = models.CharField(max_length=20, verbose_name="开始桩号", help_text="开始桩号", null=True, blank=True)
    endplno = models.CharField(max_length=20, verbose_name="结束桩号", help_text="结束桩号", null=True, blank=True)
    iscontrol = models.BooleanField(verbose_name="是否属于控制设备", help_text="是否属于控制设备", null=True, blank=True)
    plmark = models.CharField(max_length=5, verbose_name="桩号标志名", help_text="桩号标志名", null=True, blank=True)
    startplpoint = models.FloatField(verbose_name=" 桩号开始里程", help_text="桩号开始里程", null=True, blank=True)
    endplpoint = models.FloatField(verbose_name="桩号结束里程", help_text="桩号结束里程", null=True, blank=True)
    upid = models.IntegerField(verbose_name="父ID", null=True, blank=True, help_text="父ID")
    devicecode = models.CharField(max_length=64, verbose_name="设备编号", help_text="设备编号", null=True, blank=True)
    assetscode = models.CharField(max_length=64, verbose_name="资产编号", help_text="资产编号", null=True, blank=True)
    code = models.CharField(max_length=50, verbose_name="二维码", help_text="二维码", null=True, blank=True)
    enable = models.BooleanField(verbose_name="是否启用", help_text="是否启用", null=True, blank=True)
    enabletime = models.DateTimeField(verbose_name="启用时间", help_text="启用时间", null=True, blank=True)
    controlltype = models.IntegerField(verbose_name="控制类别(1:PLC控制设备,2:网络控制设备)", null=True,
                                       help_text="控制类别(1:PLC控制设备,2:网络控制设备)")
    isvisiable = models.IntegerField(verbose_name="是否可见", help_text="是否可见", null=True, blank=True)
    modelid = models.ForeignKey(DeviceModel, db_column='modelid', on_delete=models.RESTRICT, verbose_name="设备型号ID",
                                help_text="设备型号ID", null=True, blank=True)
    structid = models.ForeignKey(Structures, db_column='structid', on_delete=models.RESTRICT, verbose_name="构造物ID",
                                 help_text="构造物ID", null=True, blank=True)
    directid = models.ForeignKey(SysDict, db_column='directid', on_delete=models.RESTRICT, verbose_name="安装方向ID",
                                 help_text="安装方向ID", null=True, blank=True)
    typeid = models.ForeignKey(DeviceTypes, db_column='typeid', on_delete=models.RESTRICT, verbose_name="设备类别ID",
                               help_text="设备类别ID", null=True, blank=True)
    alia = models.CharField(max_length=50, verbose_name="别名", help_text="别名", null=True, blank=True)
    lat = models.FloatField(verbose_name="纬度", help_text="纬度", null=True, blank=True)
    lon = models.FloatField(verbose_name="经度", help_text="经度", null=True, blank=True)

    class Meta:
        db_table = table_prefix + 'devices'
        verbose_name = "设备信息"
        verbose_name_plural = verbose_name
        # ordering = ('modelname',)


class DevicesAttr(CoreModel):
    deviceid = models.ForeignKey(Devices, db_column='deviceid', on_delete=models.RESTRICT, verbose_name="设备ID",
                                 help_text="设备ID", null=True, blank=True)
    name = models.CharField(max_length=50, verbose_name="属性名", help_text="属性名", null=True, blank=True)
    value = models.CharField(max_length=1000, verbose_name="属性值", help_text="属性值", null=True, blank=True)
    typeclass = models.ForeignKey(SysDict, db_column='typeclass', on_delete=models.RESTRICT, verbose_name="属性类别",
                                  help_text="属性类别", null=True, blank=True)

    class Meta:
        db_table = table_prefix + 'devicesattr'
        verbose_name = "设备扩充属性"
        verbose_name_plural = verbose_name


class DeviceStateLog(CoreModel):
    deviceid = models.IntegerField(verbose_name="设备id", help_text="设备id", null=True, blank=True)
    statename = models.CharField(max_length=50, verbose_name="参数名称", help_text="参数名称", null=True, blank=True)
    statevalue = models.CharField(max_length=50, verbose_name="参数值", help_text="参数值", null=True, blank=True)
    writetime = models.DateTimeField(verbose_name="记录时间", help_text="记录时间", null=True, blank=True)

    class Meta:
        db_table = table_prefix + 'devicestatelog'
        verbose_name = "设备日志"
        verbose_name_plural = verbose_name


class DeviceStates(CoreModel):
    deviceid = models.IntegerField(verbose_name="设备id", help_text="设备id", null=True, blank=True)
    statename = models.CharField(max_length=50, verbose_name="参数名称", help_text="参数名称", null=True, blank=True)
    statevalue = models.CharField(max_length=50, verbose_name="参数值", help_text="参数值", null=True, blank=True)

    class Meta:
        db_table = table_prefix + 'devicestates'
        verbose_name = "设备最新状态记录表"
        verbose_name_plural = verbose_name


class DeviceTypeAttr(CoreModel):
    devicetypeid = models.ForeignKey(DeviceTypes, db_column='devicetypeid', on_delete=models.RESTRICT,
                                     verbose_name="设备类别ID", help_text="设备类别ID", null=True, blank=True)
    name = models.CharField(max_length=50, verbose_name="属性名", help_text="属性名", null=True, blank=True)
    value = models.CharField(max_length=255, verbose_name="属性值", help_text="属性值", null=True, blank=True)
    typeclass = models.IntegerField(verbose_name="属性类别", help_text="属性类别", null=True, blank=True)

    class Meta:
        db_table = table_prefix + 'devicetypeattr'
        verbose_name = "设备类型扩充属性"
        verbose_name_plural = verbose_name


class DDL(CoreModel):
    deviceid = models.IntegerField(verbose_name="设备id", help_text="设备id", null=True, blank=True)
    updeviceid = models.IntegerField(verbose_name="上级设备id", help_text="上级设备id", null=True, blank=True)
    linktype = models.IntegerField(verbose_name="连接类型", help_text="连接类型", null=True, blank=True)

    class Meta:
        db_table = table_prefix + 'ddl'
        verbose_name = "设备关系表"
        verbose_name_plural = verbose_name


class Driverinfo(CoreModel):
    drivername = models.CharField(max_length=50, verbose_name="驱动名称", help_text="驱动名称", null=True, blank=True)
    driverpath = models.CharField(max_length=500, verbose_name="驱动路径", help_text="驱动路径", null=True, blank=True)
    driverclass = models.CharField(max_length=50, verbose_name="驱动类名", help_text="驱动类名", null=True, blank=True)
    driverid = models.IntegerField(verbose_name="驱动id", help_text="驱动id", null=True, blank=True)

    class Meta:
        db_table = table_prefix + 'driverinfo'
        verbose_name = "驱动信息表"
        verbose_name_plural = verbose_name


class DeviceCtrlLog(models.Model):
    id = models.CharField(max_length=255, primary_key=True, default=uuid.uuid4, help_text="Id", verbose_name="Id")
    ctrltime = models.DateTimeField(auto_now_add=True, null=True, blank=True, help_text="控制时间", verbose_name="创建时间")
    info = models.TextField(verbose_name='控制指令', help_text="控制指令", null=True, blank=True)
    userid = models.IntegerField(verbose_name="操作人id", help_text="操作人id", null=True, blank=True)
    username = models.CharField(max_length=45, verbose_name="操作人姓名", help_text="操作人姓名", null=True, blank=True)
    structid = models.IntegerField(verbose_name="结构物id", help_text="结构物id", null=True, blank=True)
    devicetype = models.IntegerField(verbose_name="设备类别id", help_text="设备类别id", null=True, blank=True)
    deviceid = models.ForeignKey(Devices, db_column='deviceid', on_delete=models.CASCADE, verbose_name="设备",
                               help_text="设备", null=True, blank=True)

    class Meta:
        db_table = table_prefix + 'device_ctrl_log'
        verbose_name = "设备操作指令日志表"
        verbose_name_plural = verbose_name
